﻿/**
 * ...
 * @author Lucas Duroj
 */

package getengine.core;
#if flash9
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.Lib;

import getengine.core.event.EventManager;

#end
class Engine 
{
	//Start engine
	static public function start() : Void
	{
		Engine.instance;
	}
	
	//Singelton definition
	static public var instance (getInstance, null): Engine;
	static private function getInstance() : Engine 
	{
		if (instance == null) instance = new Engine();
		return instance;
	}
	
	//Managers
	private var eventManagerRef : EventManager;
	private var sceneManagerRef : SceneManager;
	
#if flash9
	//Flash stage
	private var flashStage : Stage;
#end
	
	private function new() 
	{
		init();
	}
	
	private function init() : Void
	{
		//Init managers
		eventManagerRef = EventManager.instance;
		sceneManagerRef = SceneManager.instance;

#if flash9
		//Setup flash stage
		flashStage = Lib.current.stage;
		flashStage.scaleMode = NO_SCALE;
		flashStage.align = TOP_LEFT;
		
		flashStage.addEventListener(Event.EXIT_FRAME, onExitFrame);
#end
}
#if flash9
	private function onExitFrame(e:Event):Void 
	{
		//Event Phase
		eventManagerRef.flushAsyncEvents();
		//Frameloop
		sceneManagerRef.update();
	}
#end
}